﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GDIDB;

namespace PowerControl
{
    public partial class RadiationForm : Form
    {
        private Simulation sim;
        private DBGraphics memGraphics;


        public Simulation AttachedSimulation
        {
            get { return sim; }
            set { sim = value; }
        }

        public RadiationForm(Simulation s)
        {
            this.sim = s;
            InitializeComponent();
        }

        private void picUpdate()
        {
            List<Antenna> antennas = new List<Antenna>();
            memGraphics.g.Clear(Color.Black);

            if (checkBox2.Checked)
                for (int i = 0; i < sim.MobileDevices.Count; ++i)
                {
                    antennas.Add(sim.MobileDevices[i].Antenna);
                }

            if (checkBox1.Checked)
                antennas.Add(sim.BaseStation.Antenna);

            sim.DrawRadiationPattern(memGraphics.g, pictureBox1.Width, pictureBox1.Height, antennas, sim.WorkingFrequency, (double)trackBar1.Value, (double)trackBar2.Value, trackBar3.Value, Color.White);
            memGraphics.Render(pictureBox1.CreateGraphics());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            picUpdate();
        }

        private void RadiationForm_Load(object sender, EventArgs e)
        {
            memGraphics = new DBGraphics();
            memGraphics.CreateDoubleBuffer(pictureBox1.CreateGraphics(), pictureBox1.ClientRectangle.Width, pictureBox1.ClientRectangle.Height);

            label4.Text = trackBar1.Value.ToString() + " dB";
            label5.Text = trackBar2.Value.ToString() + " dB";
            label6.Text = trackBar3.Value.ToString() + " px";
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            label4.Text = trackBar1.Value.ToString() + " dB";
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            label5.Text = trackBar2.Value.ToString() + " dB";
        }

        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            label6.Text = trackBar3.Value.ToString() + " px";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
                picUpdate();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            picUpdate();
        }

        private void RadiationForm_SizeChanged(object sender, EventArgs e)
        {
            memGraphics.CreateDoubleBuffer(pictureBox1.CreateGraphics(), pictureBox1.ClientRectangle.Width, pictureBox1.ClientRectangle.Height);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.AddExtension = true;
            dlg.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
            dlg.Title = "Save an Image File";
            
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Bitmap bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                memGraphics.Render(Graphics.FromImage(bm));

                bm.Save(dlg.FileName);
            }
        }
    }
}
